package ac.manchester.cs.afzal.autoservd.semantic_classes;

import java.util.Iterator;
import java.util.Vector;



/**
 * This class deals with creation and populating of SQL tables for Semantic Classes
 * and their instances respectively
 * @author hamafz
 */
public class Semantic_Class_Instances
{

    String[] Algorithm_Class =
    {
        "algorithms", "alignments", "analysis", "approaches", "methods", "procedures", "searches"
    };
    String[] Application_Class =
    {
        "applications", "programs", "services", "softwares", "systems", "tools"
    };
    String[] Data_Class =
    {
        "data", "files", "records", "reports", "sequences", "trees"
    };
    String[] Data_Repository_Class =
    {
        "databases", "datasets", "libraries", "models", "repositories", "resources"
    };
    private SQL_Handler_SC sqlHandler_1 = new SQL_Handler_SC();
    private SQL_Handler_SC sqlHandler_2 = new SQL_Handler_SC();

    /**
     * This class creates the Semantic Class Tables, but currently it is not functional
     */
    public void create_semantic_class_tables()
    {
        sqlHandler_1.connect_db();
        for(int i = 0; i < Algorithm_Class.length; i ++)
        {
            String value = Algorithm_Class[i];
            if((value.endsWith("s")) &&  ! (value.equals("analysis")) &&
                     ! (value.equals("approaches")) &&  ! (value.equals("repositories")) &&
                     ! (value.equals("libraries")) &&  ! (value.equals("searches")) &&
                     ! (value.equals("data")))
            {
                value = value.substring(0, value.length() - 1);
            }
            sqlHandler_1.create_sc_tables(value);
        }
    }

    /**
     * Finds the instances of Semantic Classes from BMC_2009 Occurrences table and fills the
     * value in Semantic_Classes table
     * @param class_
     */
    public void populate_semantic_class_tables(int class_)
    {
        sqlHandler_1.initialize_parameters("jdbc:mysql://localhost:3306/BMC_2009", "root", "admin");
        sqlHandler_2.initialize_parameters("jdbc:mysql://localhost:3306/BMC_2009_2", "root", "admin");

        sqlHandler_1.connect_db();
        sqlHandler_2.connect_db();

        switch(class_)
        {
            case 0: // 0 for algorithm
                for(int i = 0; i < Algorithm_Class.length; i ++)
                {
                    String value = Algorithm_Class[i];

                    if((value.endsWith("s")) &&  ! (value.equals("analysis")) &&
                             ! (value.equals("approaches")) &&  ! (value.equals("searches")))
                    {
                        value = value.substring(0, value.length() - 1);
                    }

                    Vector instances = sqlHandler_1.get_sc_instances_from_occurences(value);
                    Iterator instancesIterator = instances.iterator();
                    while(instancesIterator.hasNext())
                    {
                        String term = instancesIterator.next().toString();
                        term = term.trim();
                        String[] doc_table1 =
                        {
                            "Value", "Type", "Keyword"
                        };
                        String[] doc_table_value1 =
                        {
                            term, "Algorithms", value
                        };
                        sqlHandler_2.insert_table("semantic_classes", doc_table1, doc_table_value1);
                    }
                }
            case 1: // 0 for Application
                for(int i = 0; i < Application_Class.length; i ++)
                {
                    String value = Application_Class[i];
                    if(value.endsWith("s"))
                    {
                        value = value.substring(0, value.length() - 1);
                    }


                    Vector instances = sqlHandler_1.get_sc_instances_from_occurences(value);
                    Iterator instancesIterator = instances.iterator();
                    while(instancesIterator.hasNext())
                    {
                        String term = instancesIterator.next().toString();
                        term = term.trim();
                        String[] doc_table1 =
                        {
                            "Value", "Type", "Keyword"
                        };
                        String[] doc_table_value1 =
                        {
                            term, "Application", value
                        };
                        sqlHandler_2.insert_table("semantic_classes", doc_table1, doc_table_value1);
                    }
                }
            case 2: // 0 for Data
                for(int i = 0; i < Data_Class.length; i ++)
                {
                    String value = Data_Class[i];
                    if(value.endsWith("s"))
                    {
                        value = value.substring(0, value.length() - 1);
                    }

                    Vector instances = sqlHandler_1.get_sc_instances_from_occurences(value);
                    Iterator instancesIterator = instances.iterator();
                    while(instancesIterator.hasNext())
                    {
                        String term = instancesIterator.next().toString();
                        term = term.trim();
                        String[] doc_table1 =
                        {
                            "Value", "Type", "Keyword"
                        };
                        String[] doc_table_value1 =
                        {
                            term, "Data", value
                        };
                        sqlHandler_2.insert_table("semantic_classes", doc_table1, doc_table_value1);
                    }
                }
            case 3: // 0 for Data Resources
                for(int i = 0; i < Data_Repository_Class.length; i ++)
                {
                    String value = Data_Repository_Class[i];

                    if((value.endsWith("s")) &&  ! (value.equals("libraries")) &&
                             ! (value.equals("repositories")))
                    {
                        value = value.substring(0, value.length() - 1);
                    }

                    Vector instances = sqlHandler_1.get_sc_instances_from_occurences(value);
                    Iterator instancesIterator = instances.iterator();
                    while(instancesIterator.hasNext())
                    {
                        String term = instancesIterator.next().toString();
                        term = term.trim();
                        String[] doc_table1 =
                        {
                            "Value", "Type", "Keyword"
                        };
                        String[] doc_table_value1 =
                        {
                            term, "Data_Repository", value
                        };
                        sqlHandler_2.insert_table("semantic_classes", doc_table1, doc_table_value1);
                    }
                }
            default:
                System.out.println("Unknown Semantic class entered");
                break;

        }
    }
}
